package com.wing.cabinet.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.response.CabinetVO;
import com.wing.cabinet.service.CabinetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.CabinetForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-刀柜管理", tags = "刀柜管理")
@RestController("appCabinetController")
@RequestMapping("cabinet/app/cabinet")
public class CabinetController {

    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "柜子列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "code", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CabinetVO> list(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            , @RequestParam(name = "limit",defaultValue = "10") Integer limit
            , @RequestParam(name = "sort",defaultValue = "id") String sort
            , @RequestParam(name = "order",defaultValue = "desc") String order
            , String code
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            , Cabinet.CabinetType cabinetType
            ,String belongCabinet
            ) {
        Page<CabinetVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("code",code);
        params.put("queryFlag",queryFlag);
        if (cabinetNature!=null){
            params.put("cabinetNature",cabinetNature.getValue());
        }
        if (cabinetType!=null){
            params.put("cabinetType",cabinetType.getValue());
        }
        params.put("belongCabinet",belongCabinet);
        Page<CabinetVO> list = cabinetService.pageList4Cab(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long cabinetId) {
        CabinetVO cabinetVO = cabinetService.get4appById(cabinetId);
        if (cabinetVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), cabinetVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CabinetForm cabinetForm) {
        if (cabinetService.add(cabinetForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }
    @ApiOperation(value = "柜子编号是否存在", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/isExistCabCode")
    public JsonResult<CabinetVO> isExistCabCode(String code,String cabName){
        return cabinetService.isExistAndUpdate(code,cabName);
    }
    @ApiOperation(value = "查询所有柜子", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cabinetNature", value = "状态:main(0,主柜);sale(1,售刀副柜);live(2,存刀副柜)", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getCabinetVOAll")
    public JsonResult<List<Cabinet>> getCabinetVOAll(Cabinet.CabinetNature cabinetNature){
        QueryWrapper<Cabinet> wrapper = new QueryWrapper<>();
        if (cabinetNature!=null){
            wrapper.eq("cabinet_nature",cabinetNature.getValue());
        }
        wrapper.select("id","code");
        List<Cabinet> list = cabinetService.list(wrapper);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询主柜", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getMainCabByAttachCabCode")
    public JsonResult<CabinetVO> getMainCabByAttachCabCode(String code){
        return JsonResult.success(cabinetService.getMainCab4appByCode(code));
    }

    @ApiOperation(value = "根据主柜编码查询主柜", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getMainCabByMainCabCode")
    public JsonResult<Cabinet> getMainCabByMainCabCode(String cabCode){
        return JsonResult.success(cabinetService.getMainCab4appByMainCabCode(cabCode));
    }

    @ApiOperation(value = "获取柜子(WX)", notes = "搜索")
    @GetMapping("/getCabs")
    public JsonResult<CabinetVO> getCabs(){
        return JsonResult.success(cabinetService.getCabs());
    }

    @ApiOperation(value = "根据设备编码查询主柜", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceNo", value = "设备编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getMainCabByDeviceNo")
    public JsonResult<Cabinet> getMainCabByDeviceNo(String deviceNo){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("device_no", deviceNo);
        return JsonResult.success(cabinetService.getOne(queryWrapper));
    }

}
